<template>
  <el-dropdown tabindex="99999" trigger="hover">
    <span class="el-dropdown-link">
      <img
        ref="userRef"
        v-show="props.showUser"
        class="user-avatar"
        v-lazy="userStore_.userInfo?.avatarUrl"
        alt=""
        @click="router.push('/info')"
      />
    </span>
    <template #dropdown>
      <el-dropdown-menu>
        <el-dropdown-item @click="router.push('/info')"
          ><span>
            <el-icon><User /></el-icon>个人信息</span
          ></el-dropdown-item
        >
        <el-dropdown-item @click="router.push('/writeArticle')"
          ><span>
            <el-icon><EditPen /></el-icon>写文章</span
          ></el-dropdown-item
        >
        <el-dropdown-item divided @click="logout"
          ><span>
            <el-icon><SwitchButton /></el-icon>
            退出</span
          ></el-dropdown-item
        >
      </el-dropdown-menu>
    </template>
  </el-dropdown>
</template>

<script lang="ts" setup>
import { SwitchButton, EditPen, User } from "@element-plus/icons-vue";
import userStore from "@/store/user";
import router from "@/router";
import local from "@/utils/local";
const userStore_ = userStore();
const props = defineProps<{
  showUser: boolean;
}>();
const logout = () => {
  local.delete("pinia-userStore");
  router.push("/");
  userStore_.$reset();
};
</script>

<style scoped lang="less">
.user-avatar {
  position: relative;
  z-index: 999;
  width: 36px;
  height: 36px;
  border-radius: 50%;
  margin-right: 15px;
}
</style>
